package com.woniuxy.thred.syn;

/**
 * description: UnsafeTest01 <br>
 * date: 2022/2/5 11:55 下午 <br>
 * author: shesaifei <br>
 * version: 1.0 <br>
 */

/**
 * 线程不安全：数据负数、相同
 */
public class UnsafeTest01 {
    public static void main(String[] args) {
        //一份资源
        UnsafeWeb123456 web = new UnsafeWeb123456();
        //多个代理
        System.out.println(Thread.currentThread().getName());
        new Thread(web, "zs").start();
        new Thread(web, "ls").start();
        new Thread(web, "ww").start();
    }
}

class UnsafeWeb123456 implements Runnable {
    //票数
    private int ticketNums = 10;
    private boolean flag = true;

    @Override
    public void run() {
        while (flag) {
            test();
        }
    }

    public void test() {
        if (ticketNums < 0) {
            flag = false;
            return;
        }
        //模拟延时
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "-->" + ticketNums--);
    }
}